﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DAO;
using QLVBDQ.Mo_Rong;
using QLVBDQ.Cua_Hang;

namespace QLVBDQ
{
    public enum WindowsType
    {
        LapPhieuBanHang,
        LapPhieuNo,
        LapPhieuMuaHang,
        LapPhieuDichVu,
        LapPhieuGiaCong,
        LapBaoCaoTon,
        LapBaoCaoCongNo,
        LapPhieuChi,

        ThongTinKhachHang,
        TimKiemKhachHang,

        SoNo,
        TimKiemSoNo,

        ThongTinSanPham,
        TimKiemSanPham,

        ThongTinTho,
        TimKiemTho,

        LoaiSanPham,
        DichVu,
        Phi,
        HangGiaCong,

    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int numberOfTab = 0;
        private int indexTab = 0;
        private Dictionary<int, UserControl> listUserControl = new Dictionary<int, UserControl>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void btnLapPhieuBanHang_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.LapPhieuBanHang);
        }

        private void sortLapBaoCaoCongNo_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.LapBaoCaoCongNo);
        }

        private void sortLapBaoCaoTon_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.LapBaoCaoTon);
        }

        private void sortLapPhieuGiaCong_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.LapPhieuGiaCong);
        }

        private void sortLapPhieuNo_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.LapPhieuNo);
        }

        private void sortLapPhieuDichVu_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.LapPhieuDichVu);
        }

        private void sortLapPhieuBanHang_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.LapPhieuBanHang);
        }

        private void sortLapPhieuMuaHang_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.LapPhieuMuaHang);
        }
        int[] a = new int[10];
        private void AddItem(WindowsType type)
        {
            if (indexTab > 0)
                ((TabUserControl)tabControlManager.Children[indexTab]).Update(false);

            numberOfTab = tabControlManager.Children.Count;
            indexTab = numberOfTab;
            TabUserControl tab = new TabUserControl(type, this);
            tab.bIsEnable = true;
            tab.iID = numberOfTab;
            if (numberOfTab>=10)
            {
                return;
            }
            a[numberOfTab] = numberOfTab;


            if (numberOfTab > 0)
                ((TabUserControl)tabControlManager.Children[numberOfTab - 1]).Update(false);

            tabControlManager.Children.Add(tab);

            switch (type)
            {
                case WindowsType.LapPhieuBanHang:
                    listUserControl.Add(numberOfTab, new LapPhieuBanHang());
                    break;
                case WindowsType.LapPhieuNo:
                    listUserControl.Add(numberOfTab, new LapPhieuNo());
                    break;
                case WindowsType.LapPhieuMuaHang:
                    listUserControl.Add(numberOfTab, new LapPhieuMuaHang());
                    break;
                case WindowsType.LapPhieuDichVu:
                    listUserControl.Add(numberOfTab, new LapPhieuDichVu());
                    break;
                case WindowsType.LapPhieuGiaCong:
                    listUserControl.Add(numberOfTab, new LapPhieuGiaCong());
                    break;
                case WindowsType.LapBaoCaoTon:
                    listUserControl.Add(numberOfTab, new LapBaoCaoTon());
                    break;
                case WindowsType.LapBaoCaoCongNo:
                    listUserControl.Add(numberOfTab, new LapBaoCaoCongNo());
                    break;
                case WindowsType.ThongTinKhachHang:
                    listUserControl.Add(numberOfTab, new ThongTinKhachHang());
                    break;
                case WindowsType.TimKiemKhachHang:
                    listUserControl.Add(numberOfTab, new TimKiemKhachHang());
                    break;
                case WindowsType.SoNo:
                    listUserControl.Add(numberOfTab, new ThongTinNo());
                    break;
                case WindowsType.TimKiemSoNo:
                    break;
                case WindowsType.ThongTinSanPham:
                    listUserControl.Add(numberOfTab, new ThongTinSanPham());
                    break;
                case WindowsType.TimKiemSanPham:
                    listUserControl.Add(numberOfTab, new TimKiemSanPham());
                    break;
                case WindowsType.ThongTinTho:
                    listUserControl.Add(numberOfTab, new ThongTinTho());
                    break;
                case WindowsType.TimKiemTho:
                    listUserControl.Add(numberOfTab, new TimKiemTho());
                    break;
                case WindowsType.DichVu:
                    listUserControl.Add(numberOfTab, new DichVu());
                    break;
                case WindowsType.LoaiSanPham:
                    listUserControl.Add(numberOfTab, new LoaiSanPham());
                    break;
                case WindowsType.HangGiaCong:
                    listUserControl.Add(numberOfTab, new HangGiaCong());
                    break;
                case WindowsType.Phi:
                    listUserControl.Add(numberOfTab, new Phi());
                    break;
                case WindowsType.LapPhieuChi:
                    listUserControl.Add(numberOfTab, new LapPhieuChi());
                    break;
                    
                default:
                    break;
            }
            
            rootManager.Children.Clear();
            rootManager.Children.Add(listUserControl[numberOfTab]);
        }

        public void RemoveItem(int id)
        {
            numberOfTab = tabControlManager.Children.Count;
            if (id != indexTab)
            {
                for (int i = id + 1; i < numberOfTab; i++ )
                {
                    ((TabUserControl)tabControlManager.Children[i]).iID = i - 1;
                    listUserControl.Remove(i - 1);
                    listUserControl.Add(i - 1, listUserControl[i]);

                    a[i] = i - 1;
                }
                listUserControl.Remove(numberOfTab - 1);
                tabControlManager.Children.RemoveAt(id);
                if (indexTab > id)
                    indexTab--;
            }
        }

        public void EnableItem(int id)
        {
            ((TabUserControl)tabControlManager.Children[indexTab]).Update(false);
            ((TabUserControl)tabControlManager.Children[id]).Update(true);
            rootManager.Children.Clear();
            rootManager.Children.Add(listUserControl[id]);

            indexTab = id;
        }

        private void btnDsKhachHang_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.ThongTinKhachHang);
        }

        private void btnTimKiemSanPham_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.TimKiemSanPham);
        }

        private void btnDsTho_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.ThongTinTho);
        }

        private void btnTimKiemTho_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.TimKiemTho);
        }

        private void btnThongTin_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void btnLapPhieuDichVu_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.LapPhieuChi);
        }

        private void btnSoNo_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.SoNo);
        }

        private void btnDsSanPham_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.ThongTinSanPham);
        }

        private void btnTimKiemSoNo_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.TimKiemSoNo);
        }

        private void btnTimKiemKhachHang_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.TimKiemKhachHang);
        }

        /// <summary>
        /// Hàm load lúc khởi tạo
        /// Khởi tạo SQLConnection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            // Khởi tạo SqlConnection
            SQLConnection sql = SQLConnection.Instance;
        }

        private void btnLoaiSp_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.LoaiSanPham);
        }

        private void btnDichVu_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.DichVu);
        }

        private void btnPhi_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.Phi);
        }

        private void btnHangGC_Click(object sender, RoutedEventArgs e)
        {
            this.AddItem(WindowsType.HangGiaCong);
        }

        private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
        {
            SQLConnection.Instance.DisConnect();
        }
    }
}
